<?php

namespace IDock\Insurance\ACIC\Response;

use IDock\Http\Response;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\VehicleActualValueVisitor;

class VehicleActualValueResponse extends AllACICResponse
{

    /**
     * @var VehicleActualValueVisitor $visitor
     */
    public $visitor;

    /**
     * @var VehicleActualValuePrinter $printer
     */
    public $printer;

    public function setVisitor($visitor): void
    {
        $this->visitor = $visitor;
    }

    public function printerInit(): Response
    {
        $this->printer = new VehicleActualValuePrinter();
        
        $vehiclePrice = (int)$this->visitor->vehiclePrice;

        $monthDiff = (int)$this->visitor->monthDiff;

        $this->actualRate();

        if (!$monthDiff or !$this->printer->actualRate) {
            return $this;
        }

        $actualValue = $vehiclePrice * (1 - (float)($monthDiff * $this->printer->actualRate));

        $this->printer->actualValue = $actualValue;

        $actualValueMostMin = $vehiclePrice * 0.2;

        $this->printer->actualValueMax = round(max($actualValue * 1.3,$actualValueMostMin),2);

        $this->printer->actualValueMin = round(max($actualValue * 0.7, $actualValueMostMin),2);

        $this->printer->actualValue = round(max($this->printer->actualValue, $this->printer->actualValueMin),2);

        $this->printer->actualRate =  (string)round($this->printer->actualRate * 100,2);

        return $this;
    }
    
    private function actualRate(){
        $carKindCode = $this->visitor->carKindCode;
        $useNatureCode = $this->visitor->useNatureCode;
        $causeType = $this->visitor->causeType;
        $vehicleSeat = $this->visitor->vehicleSeat;

        $vehiclePrice = $this->visitor->vehiclePrice;
        $energyType = $this->visitor->energyType;
        
        //默认
        $this->printer->actualRate = '0.0060';

        //非新能源
        if (!$this->visitor->isNewEnergy) {
            if ($vehicleSeat <= 9 and $carKindCode == '100') {
                //9座及9座以下客车
                //家庭自用 非营业
                if ($causeType == '001' or $useNatureCode == '01') {
                    $this->printer->actualRate = '0.0060';
                    //出租
                } elseif ($causeType == '004') {
                    $this->printer->actualRate = '0.0110';
                    //其他
                } else {
                    $this->printer->actualRate = '0.0090';
                }
                return;
            } elseif ($vehicleSeat > 10 and $carKindCode == '100') {
                //10座及10座以上客车
                //家庭自用 非营业
                if ($causeType == '001' or $useNatureCode == '01') {
                    $this->printer->actualRate = '0.0090';
                    //出租
                } elseif ($causeType == '004') {
                    $this->printer->actualRate = '0.0110';
                    //其他
                } else {
                    $this->printer->actualRate = '0.0090';
                }
            } elseif ($carKindCode == '210' or $carKindCode == '200') {
                if ($useNatureCode == '01') {
                    $this->printer->actualRate = '0.0090';
                    //其他
                } else {
                    $this->printer->actualRate = '0.0110';
                }
            }
            return;
        }

        //新能源
        if ($this->visitor->isNewEnergy) {

            //插电式混合动力与燃料电池
            if ($energyType == 'E04' or $energyType == 'E03') {
                $this->printer->actualRate = '0.0063';
                return;
            }


            if ($vehicleSeat <= 9 and $carKindCode == '100') {
                //9座及9座以下客车
                //家庭自用 非营业
                if ($causeType == '001' or $useNatureCode == '02') {
                    if ($vehiclePrice < 100000) {
                        $this->printer->actualRate = '0.0082';
                    }
                    if ($vehiclePrice >= 100000 and $vehiclePrice < 200000) {
                        $this->printer->actualRate = '0.0077';
                    }
                    if ($vehiclePrice >= 200000 and $vehiclePrice < 300000) {
                        $this->printer->actualRate = '0.0072';
                    }
                    if ($vehiclePrice >= 300000) {
                        $this->printer->actualRate = '0.0068';
                    }
                }
                return;
            }
            //9座及9座以下客车
            if ($vehicleSeat > 10 and $carKindCode == '100') {
                //出租
                if ($causeType == '004') {
                    $this->printer->actualRate = '0.0110';
                } else {
                    $this->printer->actualRate = '0.0090';
                }
                return;
            }

            //载货汽车
            if ($carKindCode == '200') {
                //非营业
                if ($useNatureCode == '01') {
                    $this->printer->actualRate = '0.0090';
                } else {
                    $this->printer->actualRate = '0.0110';
                }
                return;
            }
            //低速货车
            if ($carKindCode == '210') {
                //非营业
                if ($useNatureCode == '01') {
                    $this->printer->actualRate = '0.0110';
                } else {
                    $this->printer->actualRate = '0.0140';
                }
                return;
            }
        }
    }
}
